<template>
  <div>
    <van-form @submit="onSubmit" v-loading="loading" style="margin-top: 40px;">
      <van-field label="任务内容">
        <template #input>
          <span>{{model.content}}</span>
        </template>
      </van-field>
      <van-field label="接收人">
        <template #input>
          <span>{{model.to_person_name}}</span>
        </template>
      </van-field>
      <van-field label="重要程度">
        <template #input>
          <span>{{model.zycd}}</span>
        </template>
      </van-field>
      <van-field label="状态">
        <template #input>
          <span>{{model.status}}</span>
          <van-image  height="24" style="margin-left: 20px;" @click="showStatusEdit()"
                     src="/images/mob-icon-vf-04.svg"/>
        </template>
      </van-field>
      <van-field label="要求完成时间">
        <template #input>
          <span>{{model.jh_end_time_str}}</span>
        </template>
      </van-field>
      <van-field label="实际完成时间">
        <template #input>
          <span>{{model.sj_end_time_str}}</span>
          <van-image  height="24" style="margin-left: 20px;" @click="showTimeEdit()"
                      src="/images/mob-icon-vf-04.svg"/>
        </template>
      </van-field>
      <van-field v-model="model.remark" label="备注"></van-field>
      <div style="margin: 16px;margin-top: 30px;">
        <van-button block type="info" native-type="submit"
                    style="height: 42px;font-size: 18px;color:#ffffff;background-color:#028aff;border-radius: 4px;">
          提交</van-button>
      </div>
    </van-form>

    <van-popup v-model="timeEditShow" position="bottom" :style="{height:'100%'}" closeable>
      <div style="margin-top: 40px;">
        <van-datetime-picker v-model="timeEditValue" @confirm="timeEdit_setValue" @cancel="timeEditShow=false"
                             type="datetime" title="选择日期时间" :min-date="minDate"/>
      </div>
    </van-popup>
    <van-popup v-model="statusEditShow" position="bottom" :style="{height:'100%'}" closeable>
      <div style="margin-top: 40px;">
        <van-picker :default-index="statusIndexInList" :columns="statusList"
                    @confirm="statusEdit_setValue" @cancel="statusEditShow=false" title="选择" show-toolbar />
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:true,

      model:{},

      minDate: new Date(2025, 0, 1),

      timeEditShow:false,
      timeEditValue:false,

      statusEditShow:false,
      statusIndexInList:0,
      statusList:['未开始','进行中','已完成'],
    }
  },
  props: {
    task_id: { type: String, default: null },
  },
  created() {},
  mounted() {
    this.minDate=new Date();
    this.load();
  },
  methods: {
    async load(){
      this.loading = true;
      if(this.em(this.task_id)){return}
      this.model = await elm.post('/business/pmis/rwzp/GetOneByTaskId', {'task_id':this.task_id});
      this.model['jh_end_time_str'] = this.em(this.model.jh_end_time)?'':this.model.jh_end_time.replace('T',' ');
      this.model['sj_end_time_str'] = this.em(this.model.sj_end_time)?'':this.model.sj_end_time.replace('T',' ');
      this.model = JSON.parse(JSON.stringify(this.model));
      this.loading = false;
    },
    showTimeEdit:function(){
      this.timeEditValue = this.model.sj_end_time_str;
      this.timeEditShow = true;
    },
    timeEdit_setValue:function(value){
      this.model.sj_end_time = this.turnDate01(value);
      this.model.sj_end_time_str = this.model.sj_end_time;
      this.timeEditShow = false;
    },

    showStatusEdit:function(){
      this.statusIndexInList = 0;
      for(let i=0;i<this.statusList.length;i++){
        if(this.statusList[i] == this.model.status){
          this.statusIndexInList = i;
          break;
        }
      }
      this.statusEditShow = true;
    },
    statusEdit_setValue:function(value){
      this.model.status = value;
      this.statusEditShow = false;
    },

    async onSubmit() {
      this.loading = true;

      //保存业务数据
      let result = await elm.post('/business/pmis/rwzp/Upsert', this.model);
      if (result == true) {
        this.$toast.success({ type: 'success', message: '保存成功' });
      }
      else { this.$toast.fail('保存失败，请稍后尝试。'); }

      this.$emit('after_submit', result);
      this.loading = false;
    },

    turnDate01:function(d){
      if(d==null || d.getFullYear==null){return '';}
      let year = d.getFullYear();
      let month = d.getMonth()+1;
      let day = d.getDate();
      let hour = d.getHours();
      let minute = d.getMinutes();
      let second = d.getSeconds();

      let s = year + '-' + this.addZero(month) + '-' + this.addZero(day) + ' '
          + this.addZero(hour) + ':' + this.addZero(minute) + ':' + this.addZero(second);
      return  s;
    },
    addZero:function(s){ if((''+s).length==1){return ('0'+s);} else{return s;} },

    em: function (s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },
  }
}
</script>